package org.ruiyichen.guizhou.agr.ui.mineview;

/**
 * Created by Administrator
 */

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.utils.StrUtil;


public class CustomDialog extends Dialog {

    public interface Style {
        /**
         * 强制更新
         */
        int MANDATORY_UPDATE = 0;

        /**
         * 普通更新
         */
        int REGULAR_UPDATE = 1;

        /**
         * 普通提示
         */
        int GENERAL_TIPS = 2;
        /**
         * 确认类提示
         */
        int CONFIRMATION_TIPS = 3;
        /**
         * 确认类提示
         */
        int MULTI_TIPS = 4;
        /**
         * 强制确认
         */
        int FORCED_CONFIRMATION = 5;
    }

    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private int type = Style.GENERAL_TIPS;
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this(context, Style.GENERAL_TIPS);
        }

        public Builder(Context context, int type) {
            this.type = type;
            this.context = context;
        }

        public Builder setType(int type) {
            this.type = type;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param message
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * 确定
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * 确定
         *
         * @param listener
         * @return
         */
        public Builder setPositiveButton(OnClickListener listener) {
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }


        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(OnClickListener listener) {
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomDialog dialog = new CustomDialog(context, R.style.custom_dialog);
            View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            // set the dialog title
            TextView titleTV = (TextView) layout.findViewById(R.id.title);
            if (StrUtil.isNotEmpty(title)) {
                titleTV.setText(title);
            }
            //确定
            TextView leftTV = (TextView) layout.findViewById(R.id.tv_negative);

            //取消
            TextView rightTV = (TextView) layout.findViewById(R.id.tv_positive);

            //分割线
            View divisionV = layout.findViewById(R.id.v_division);

            dialog.setCanceledOnTouchOutside(type != Style.MANDATORY_UPDATE);
            switch (type) {
                //确认提示
                case Style.CONFIRMATION_TIPS:
                    leftTV.setTextColor(context.getResources().getColor(R.color.dialog_font_light_blue));
                    rightTV.setTextColor(context.getResources().getColor(R.color.dialog_font_dark_blue));
                    break;
                //一般提示
                case Style.FORCED_CONFIRMATION:
                    dialog.setCanceledOnTouchOutside(false);
                case Style.GENERAL_TIPS:
                    leftTV.setVisibility(View.GONE);
                    divisionV.setVisibility(View.GONE);
                    rightTV.setTextColor(context.getResources().getColor(R.color.dialog_font_light_blue));
                    break;
                //强制更新
                case Style.MANDATORY_UPDATE:
                    leftTV.setText("退出");
                    rightTV.setTextColor(context.getResources().getColor(R.color.dialog_font_dark_blue));
                    rightTV.setText("立即更新");
                    break;
                //一般更新
                case Style.REGULAR_UPDATE:
                    leftTV.setText("以后再说");
                    rightTV.setTextColor(context.getResources().getColor(R.color.dialog_font_dark_blue));
                    rightTV.setText("立即更新");
                    break;
            }

            if (StrUtil.isNotEmpty(message)) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
            }
            // set the cancel button
            if (StrUtil.isNotEmpty(positiveButtonText)) {
                leftTV.setText(positiveButtonText);
            }
            // set the confirm button
            if (StrUtil.isNotEmpty(negativeButtonText)) {
                rightTV.setText(negativeButtonText);
            }
            if (positiveButtonClickListener != null) {
                leftTV.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        positiveButtonClickListener.onClick(dialog,
                                DialogInterface.BUTTON_POSITIVE);
                    }
                });
            } else {
                leftTV.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }

            if (negativeButtonClickListener != null) {
                rightTV.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        negativeButtonClickListener.onClick(dialog,
                                DialogInterface.BUTTON_NEGATIVE);
                    }
                });
            } else {
                rightTV.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
            if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }

    @Override
    public void onBackPressed() {
//        if (type != Style.MANDATORY_UPDATE)
//            super.onBackPressed();
    }
}
